package com.zjz.demo.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/08/17:28
 * @Description:
 * @Target:
 */
public class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
/*
        new Thread(new Runnable()).start();
        new Thread(new FutureTask<V>()).start();
        new Thread(new FutureTask<V>(Callable)).start();
        */


        new Thread("HHH").start();  // 怎么启动Callable


        MyThread thread = new MyThread();
        FutureTask futureTask = new FutureTask(thread);   // 利用FutureTask 将我们写的实现类包装,然后Thread执行
        new Thread(futureTask,"A").start();  // Runnable直接包装就可以了,所以lambda
        new Thread(futureTask,"B").start();  // 结果被缓存,提高效率

        String ss = (String) futureTask.get();  // 获取Callable的返回结果  可能会阻塞,把它放在最后  或者使用异步通信
        System.out.println(ss);




    }

}


class MyThread implements Callable<String>{
    @Override
    public String call() throws Exception {
        return "123";

    }
}

